﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Mmht.BT;
using System.IO;
using XpdfRasterizer;

/// <summary>
/// ImagePdf 的摘要说明
/// </summary>
public class ImagePdf
{
    ImagePdfBT ip = new ImagePdfBT();

    private bool SavePdfImage(out int numPages, string pdf_url, string image_url)
    {
        numPages = 0;
        bool save = false;
        //图片像素
        double dpi = 20;
        XpdfRasterizerClass rast = new XpdfRasterizerClass();
        try
        {
            string pu = System.Web.HttpContext.Current.Server.MapPath(pdf_url);
            rast.loadFile(pu);//pdf addresss
        }
        catch
        {
            return false;
        }

        string bu = System.Web.HttpContext.Current.Server.MapPath(image_url);
        try
        {
            numPages = rast.numPages;
            rast.writePageBitmap(1, dpi, rast.imageRGB, rast.imageFileBMP, bu);
            save = true;
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.Message);
            save = false;
        }
        finally
        {
            rast.closeFile();
        }
        return save;
    }
    /// <summary>
    /// 根据pdfName和type为pdf创建缩略图
    /// </summary>
    /// <param name="pdfName"></param>
    public void CreatePdfImageByName(string pdfName, string type)
    {
        GetPdf gp = new GetPdf();
        DataTable dt = gp.GetPdfByName(pdfName, type);
        if (dt == null)
        {
            return;
        }
        if (dt.Rows.Count == 0)
        {
            return;
        }
        DataRow dr = dt.Rows[0];
        string pdf_url = dr["pdf_url"].ToString();
        string image_url = "files/pdf_images/";
        image_url += dr["id"].ToString();
        image_url += ".bmp";
        int numPages = 0;
        if (SavePdfImage(out numPages, pdf_url, image_url))
        {
            dr["image_url"] = image_url;
            dr["num_pages"] = numPages.ToString();
        }
        else
        {
            return;
        }
        ip.SaveImageUrl(dt);
        return;
    }
    /// <summary>
    /// 为某type的所有pdf创建缩略图
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public string CreatePdfImage(string type)
    {
        string message = string.Empty;
        int success = 0;
        int failure = 0;
        DataTable dt = new DataTable();
        dt = ip.GetPdfUrl(type);
        if (dt == null || dt.Rows.Count == 0)
        {
            message = "数据不存在，请重新选择更新数据！";
            return message;
        }
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string pdf_url = dt.Rows[i]["pdf_url"].ToString();
            string image_url = "files/pdf_images/";
            image_url += dt.Rows[i]["id"].ToString();
            image_url += ".bmp";
            int numPages = 0;
            if (SavePdfImage(out numPages, pdf_url, image_url))
            {
                dt.Rows[i]["image_url"] = image_url;
                dt.Rows[i]["num_pages"] = numPages.ToString();
                success++;
            }
            else
            {
                failure++;
            }
        }
        message = "成功生成缩略图"+success.ToString()+"个，失败"+failure.ToString()+"个。";
        ip.SaveImageUrl(dt);
        return message;
    }

    public void DeletetPdfImage()
    {
        string fold_url = "files/pdf_images";
        fold_url = System.Web.HttpContext.Current.Server.MapPath(fold_url);
        foreach (string filepath in Directory.GetFiles(fold_url, "*.bmp", SearchOption.AllDirectories))
        {
            FileInfo fi = new FileInfo(filepath);
            fi.Delete();
        }

    }

}
